package com.baizhi.zxt.controller;

import com.baizhi.zxt.entity.Admin;
import com.baizhi.zxt.service.AdminServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("addAdmin")
    @ResponseBody
    public String addAdmin(@RequestBody Admin admin){
        adminService.register(admin);
        return "";
    }

    @RequestMapping("getImageCodes")
    @ResponseBody
    public Map<String,String> getImageCodes(){
        return adminService.getImageCodes();
    }

    @RequestMapping("login")
    @ResponseBody
    public Map<String,String> login(@RequestBody Admin admin, String token){
        return adminService.login(admin,token);
    }

    //从redis中根据token获取登录信息
    @RequestMapping("getUserInfo")
    @ResponseBody
    public Admin getUserInfo(String token){
        return adminService.getUserInfo(token);
    }

    //从redis中根据token删除登录信息
    @RequestMapping("logout")
    @ResponseBody
    public void logout(String token){
        adminService.logout(token);
    }
}
